package com.hifar.test.syncData.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.constant.SystemConsts;

import com.hifar.test.syncData.enums.SyncDataStrategyEnum;
import com.hifar.test.syncData.mapper.HfEnvSyncTableConfigMapper;
import com.hifar.test.syncData.pojo.HfEnvSyncTableConfig;
import com.hifar.test.syncData.pojo.VO.HfEnvSyncTableConfigAddVO;
import com.hifar.test.syncData.pojo.VO.TableFieldVO;
import com.hifar.test.syncData.service.IHfEnvSyncTableConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.*;

/**
 * @author zhuWeiWei
 * @description TODO
 * @date 2025/4/11 12:00
 */
@Service
@Slf4j
public class HfEnvSyncTableConfigServiceImpl extends ServiceImpl<HfEnvSyncTableConfigMapper, HfEnvSyncTableConfig>
        implements IHfEnvSyncTableConfigService {


    @Override
    public void batchAdd(List<HfEnvSyncTableConfigAddVO> tableConfigAddVOList) {
        List<HfEnvSyncTableConfig> insertList = new ArrayList<>(tableConfigAddVOList.size());
        for (HfEnvSyncTableConfigAddVO hfEnvSyncTableConfigAddVO : tableConfigAddVOList) {
            String tableName = hfEnvSyncTableConfigAddVO.getTableName();
            String description = hfEnvSyncTableConfigAddVO.getDescription();
            HfEnvSyncTableConfig hfEnvSyncTableConfig = new HfEnvSyncTableConfig();
            hfEnvSyncTableConfig.setTableName(tableName);
            hfEnvSyncTableConfig.setDescription(description);
            boolean exist = this.tableIsExist(tableName);
            Assert.isTrue(!exist, String.format("表%s已添加!", tableName));
            hfEnvSyncTableConfig.setEnable(SystemConsts.YES);
            hfEnvSyncTableConfig.setDataSource(SystemConsts.DATASOURCE_MASTER);
            hfEnvSyncTableConfig.setStrategy(SyncDataStrategyEnum.UPDATE.getValue());
            insertList.add(hfEnvSyncTableConfig);
        }
        this.saveBatch(insertList);
    }


    @Override
    public boolean tableIsExist(String tableName) {
        QueryWrapper<HfEnvSyncTableConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvSyncTableConfig::getTableName, tableName);
        return this.count(queryWrapper) > 0;
    }


    @Override
    public List<HfEnvSyncTableConfig> listSyncTable() {
        QueryWrapper<HfEnvSyncTableConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvSyncTableConfig::getEnable, SystemConsts.YES);
        return this.list(queryWrapper);
    }

    @Override
    public void updateLastTime(String id, Date lastTime) {
        UpdateWrapper<HfEnvSyncTableConfig> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(HfEnvSyncTableConfig::getLastSyncTime, lastTime)
                .eq(HfEnvSyncTableConfig::getId, id);
        this.update(updateWrapper);
    }


    @Override
    public Page<HfEnvSyncTableConfig> listPageAllTable(Page<HfEnvSyncTableConfig> page, QueryWrapper<HfEnvSyncTableConfig> queryWrapper) {
        return baseMapper.selectAllTable(page, queryWrapper);
    }

    @Override
    public List<TableFieldVO> listTableField(String tableName) {
        return baseMapper.listTableField(tableName);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdateSyncTime(List<String> idList, Date lastSyncTime) {
        if (idList == null || idList.isEmpty()) {
            log.warn("批量更新同步时间失败：ID列表为空");
            return;
        }
        
        if (lastSyncTime == null) {
            log.warn("批量更新同步时间失败：时间为空");
            return;
        }
        
        log.info("批量更新同步时间，数量: {}, 新时间: {}", idList.size(), lastSyncTime);
        
        UpdateWrapper<HfEnvSyncTableConfig> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", idList);
        updateWrapper.set("last_sync_time", lastSyncTime);
        
        boolean result = this.update(updateWrapper);
        
        if (result) {
            log.info("批量更新同步时间成功");
        } else {
            log.error("批量更新同步时间失败");
        }
    }
}
